<template>
  <!-- 缴费流水表格 -->
  <div class="record-container">
    <!-- 查询表单 -->
    <el-card class="filter-card">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
        <el-form-item label="企业名称">
          <el-input
            v-model="queryParams.companyName"
            placeholder="请输入企业名称"
            clearable
            style="width: 200px"
            @keyup.enter="handleSearch"
          />
        </el-form-item>
        <el-form-item label="缴费时间">
          <el-date-picker
            v-model="queryParams.createTime"
            type="month"
            placeholder="请选择缴费时间"
            value-format="YYYY-MM"
            format="YYYY年MM月"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="list-card" style="margin-top: 18px">
      <el-table
        :data="paymentHistoryData"
        border
        style="width: 100%"
        v-loading="historyLoading"
      >
        <el-table-column
          prop="companyName"
          label="企业名称"
          show-overflow-tooltip
          width="400"
          align="left"
        />
        <el-table-column
          prop="contractNumber"
          label="合同编号"
          show-overflow-tooltip
          width="300"
          align="left"
        />
        <el-table-column prop="amount" label="总金额" width="120" align="left" />
        <el-table-column
          prop="remainingAmount"
          label="剩余金额"
          width="120"
          align="left"
        />
        <el-table-column prop="paidAmount" label="已付金额" width="120" align="left" />
        <el-table-column prop="status" label="状态" align="left">
          <template #default="{ row }">
            <span>{{ row.status === 0 ? "未支付" : "已支付" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" align="left" />
        <el-table-column prop="updateTime" label="修改时间" width="180" align="left" />
        <el-table-column label="操作" width="200" fixed="right" align="left">
          <template #default="{ row }">
            <el-button type="primary" link @click="viewCompanyProfile(row.companyId)">
              查看企业档案
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 缴费流水分页组件 -->
      <pagination
        v-show="historyTotal > 0"
        :total="historyTotal"
        :page="queryParams.pageNum"
        :limit="queryParams.pageSize"
        @pagination="handlePagination"
      />
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { getPaymentHistoryList } from "@/api/company/info"; // 新增导入接口
import { useRouter } from "vue-router";

// 查询参数
const queryParams = reactive({
  companyName: "",
  createTime: "",
  pageNum: 1,
  pageSize: 10,
});

// 表格数据
const tableData = ref([]);
// 分页信息
const total = ref(0);
// 加载状态
const loading = ref(false);

// 新增：缴费流水数据
const paymentHistoryData = ref([]);
const historyTotal = ref(0);
const historyLoading = ref(false);

// 添加路由实例
const router = useRouter();

// 获取缴费流水数据
const fetchPaymentHistory = async () => {
  historyLoading.value = true;
  try {
    const res = await getPaymentHistoryList(queryParams);
    if (res && res.code == 200) {
      // 防御性处理，确保rows为数组
      let rows = [];
      if (Array.isArray(res.rows)) {
        rows = res.rows;
      } else if (
        res.rows &&
        typeof res.rows === "object" &&
        typeof res.rows.length === "number"
      ) {
        rows = Array.from(res.rows);
      }
      paymentHistoryData.value = rows;
      historyTotal.value = res.total || 0;
    } else {
      paymentHistoryData.value = [];
      historyTotal.value = 0;
    }
  } catch (error) {
    console.error("获取缴费流水失败:", error);
    paymentHistoryData.value = [];
    historyTotal.value = 0;
  } finally {
    setTimeout(() => {
      historyLoading.value = false;
    }, 300);
  }
};

// 搜索
const handleSearch = () => {
  queryParams.pageNum = 1;
  fetchPaymentHistory(); // 同步刷新缴费流水数据
};

// 重置查询
const resetQuery = () => {
  queryParams.companyName = "";
  queryParams.createTime = "";
  handleSearch();
};

// 分页处理
const handlePagination = ({ page, limit }) => {
  queryParams.pageNum = page;
  queryParams.pageSize = limit;
  fetchPaymentHistory(); // 同步刷新缴费流水数据
};

// 查看企业档案
function viewCompanyProfile(companyId) {
  router.push({
    path: "/lookCompany",
    query: { companyId },
  });
}

// 组件挂载时获取数据
onMounted(() => {
  fetchPaymentHistory(); // 获取缴费流水数据
});
</script>

<style scoped>
.search-box {
  margin-bottom: 20px;
  padding: 20px;

  border-radius: 4px;
}

.record-container {
  padding: 20px;
}
</style>
